package com.mf.utils.api;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.mf.vo.myDataVo.HscpGdbhVo;
import com.mf.vo.myDataVo.HscpJdlrVo;
import com.mf.vo.myDataVo.HscpJjxsVo;
import com.mf.vo.myDataVo.HscpJnfhVo;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: lmf
 * @Create: 2024/6/8 21:37
 * Description: myData接口调用api
 */
public class MyDataApi {
    public static final String LICENCE = "f08118c961917c823a";

    /**
     * 获取从当前日期的两年前开始到现在的每个月的日期，格式为"yyyy-MM"。
     *
     * @return 包含从两年前开始到现在的每个月日期的字符串列表。
     */
    public static List<String> getDate() {
        // 获取当前日期
        LocalDate now = LocalDate.now();
        // 定义一个日期变量，为当前日期的两年前
        final LocalDate localDate = now.minusYears(2);
        // 定义日期格式化器，用于将日期格式化为"yyyy-MM"的形式
        final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM");
        // 创建一个列表，用于存储每个月的日期字符串
        List<String> monthlyDates = new ArrayList<>();
        // 从两年前的日期开始，逐月累加直到当前日期，将每个月的日期格式化后添加到列表中
        for(LocalDate date = localDate;!date.isAfter(now);date = date.plusMonths(1)){
            monthlyDates.add(date.format(formatter));
        }
        // 打印结果
        for (String date : monthlyDates) {
            System.out.println(date+"===================");
        }
        // 返回包含所有月份日期的列表
        return monthlyDates;
    }


    /**
     * 股东变化趋势
     */
    public static List<HscpGdbhVo> getHscpGdbh(String dm) {
        String url = "http://api.mairui.club/hscp/gdbh/" + dm + "/" + LICENCE;
        final String jsonString = HttpUtil.get(url);
        final JSONArray jsonArray = new JSONArray(jsonString);
        List<HscpGdbhVo> hscpGdbhVos = new ArrayList<>();
        for (int i = 0; i < jsonArray.size(); i++) {
            final HscpGdbhVo gdbhVo = new HscpGdbhVo();
            final JSONObject obj = jsonArray.getJSONObject(i);
            gdbhVo.setJzrq(obj.getStr("jzrq"));
            gdbhVo.setGdhs(obj.getStr("gdhs"));
            String bh = obj.getStr("bh");
            String addText = "新增";
            String reduceText = "减少";
            if (bh.startsWith(addText)) {
                String strNumber = bh.substring(addText.length());
                gdbhVo.setQuantity(Integer.valueOf(strNumber));
            }else if(bh.startsWith(reduceText)){
                String strNumber = bh.substring(reduceText.length());
                gdbhVo.setQuantity(Integer.valueOf(strNumber));
            }
            hscpGdbhVos.add(gdbhVo);
        }
        return hscpGdbhVos;
    }

    /**
     * 查询股票近一年各个季度的利润
     */
    public static List<HscpJdlrVo> getHscpJdlr (String dm){
        String url = "http://api.mairui.club/hscp/jdlr/" + dm + "/" + LICENCE;
        final String jsonString = HttpUtil.get(url);
        final JSONArray jsonArray = new JSONArray(jsonString);
        List<HscpJdlrVo> hscpJdlrVos = new ArrayList<>();
        for (int i = 0; i < jsonArray.size(); i++) {
            final HscpJdlrVo jdlrVo = new HscpJdlrVo();
            final JSONObject obj = jsonArray.getJSONObject(i);
            jdlrVo.setDate(obj.getStr("date"));
            jdlrVo.setIncome(obj.getBigDecimal("income"));
            jdlrVo.setExpend(obj.getBigDecimal("expend"));
            jdlrVo.setProfit(obj.getBigDecimal("profit"));
            jdlrVo.setTotalp(obj.getBigDecimal("totalp"));
            jdlrVo.setReprofit(obj.getBigDecimal("reprofit"));
            jdlrVo.setBasege(obj.getBigDecimal("basege"));
            jdlrVo.setEttege(obj.getBigDecimal("ettege"));
            jdlrVo.setOtherp(obj.getBigDecimal("otherp"));
            jdlrVo.setTotalp(obj.getBigDecimal("totalp"));
            hscpJdlrVos.add(jdlrVo);
        }
        return hscpJdlrVos;
    }
    /**
     * 解禁限售
     */
    public static List<HscpJjxsVo> getHscpJjxs (String dm){
        String url = "http://api.mairui.club/hscp/jjxs/" + dm + "/" + LICENCE;
        final String jsonString = HttpUtil.get(url);
        final JSONArray jsonArray = new JSONArray(jsonString);
        List<HscpJjxsVo> hscpJjxsVos = new ArrayList<>();
        for (int i = 0; i < jsonArray.size(); i++) {
            final HscpJjxsVo jdlrVo = new HscpJjxsVo();
            final JSONObject obj = jsonArray.getJSONObject(i);
            jdlrVo.setRdate(obj.getStr("rdate"));
            jdlrVo.setRamount(obj.getBigDecimal("ramount"));
            jdlrVo.setRprice(obj.getBigDecimal("rprice"));
            jdlrVo.setBatch(obj.getBigDecimal("batch"));
            jdlrVo.setPdate(obj.getStr("pdate"));
            hscpJjxsVos.add(jdlrVo);
        }
        return hscpJjxsVos;
    }

    /**
     * 近年分红
     */
    public static List<HscpJnfhVo> getHscpJnfh (String dm){
        String url = "http://api.mairui.club/hscp/jnfh/" + dm + "/" + LICENCE;
        final String jsonString = HttpUtil.get(url);
        final JSONArray jsonArray = new JSONArray(jsonString);
        List<HscpJnfhVo> vos = new ArrayList<>();
        for (int i = 0; i < jsonArray.size(); i++) {
            final HscpJnfhVo vo = new HscpJnfhVo();
            final JSONObject obj = jsonArray.getJSONObject(i);
            vo.setSdate(obj.getStr("sdate"));
            vo.setGive(obj.getStr("give"));
            vo.setChange(obj.getStr("change"));
            vo.setSend(obj.getStr("send"));
            vo.setLine(obj.getStr("line"));
            vo.setCdate(obj.getStr("cdate"));
            vo.setEdate(obj.getStr("edate"));
            vo.setHdate(obj.getStr("hdate"));
            vos.add(vo);
        }
        return vos;
    }
}
